//格式化时间
//dateTime：时间戳
function getFormtTime(dateTime,flag) {
    if(dateTime != null ) {
        //若传入的dateTime为字符串类型，需要进行转换成数值，若不是无需下面注释代码
        var time = parseInt(dateTime)
        var date = new Date(time);
        //获取年份
        var YY = date.getFullYear();
        //获取月份
        var MM = (date.getMonth() + 1 < 10 ? '0'+(date.getMonth() + 1) : date.getMonth() + 1);
        //获取日期
        var DD = (date.getDate() < 10 ? '0'+date.getDate() : date.getDate());
        if(flag) { //flag为true，显示时分秒格式
            //获取小时
            var hh = (date.getHours() < 10 ? '0'+date.getHours() : date.getHours());
            //获取分
            var mm = (date.getMinutes() < 10 ? '0'+date.getMinutes() : date.getMinutes());
            ///获取秒
            var ss = (date.getSeconds() < 10 ? '0'+date.getSeconds() : date.getSeconds());
            //返回时间格式： 2020-11-09 13:14:52
            return YY + '-' + MM + '-' + DD + ' ' + hh + ':' + mm + ':' + ss;
        } else {
            //返回时间格式： 2020-11-09
            return YY + '-' + MM + '-' + DD;
        }
    } else {
        return "";
    }
}

/**
 * 连小龙
 * 直接发送ajax,渲染页面
 */
let pagenum=1;
let totalrows;
let totalPage;
let n=1;
$(document).ready(function (){
    queryAllProducts();
    queryType();

});

function queryAllProducts(){
    $.ajax({
        url:"/product/queryAllProduct",
        type:"post",
        data:{pagenum:n},
        dataType:"json",
        success:function (r){
            totalPage = r.totalPage;
            var str = "<tr>" +
                "          <th>序号</th>" +
                "          <th>品牌名称</th>" +
                "          <th>商品类型</th>" +
                "          <th>商品型号</th>" +
                "          <th>单位</th>" +
                "          <th>状态</th>" +
                "          <th>创建时间</th>" +
                "          <th>创建人</th>" +
                "          <th>操作</th>" +
                "        </tr>";
            for (let i = 0; i < r.data.length; i++) {
                var status = r.data[i].productStatus;
                if (status=="0") {
                    str += "<tr>" +
                        "<td>" + r.data[i].productId + "</td>" +
                        "<td>" + r.data[i].brandName + "</td>" +
                        "<td>" + r.data[i].typeName + "</td>" +
                        "<td>" + r.data[i].productModel + "</td>" +
                        "<td>" + r.data[i].productUnit+"</td>" +
                        "<td>不可用</td>" +
                        "<td>" + getFormtTime(r.data[i].createTime,false) + "</td>" +
                        "<td>" + r.data[i].uname + "</td>" +
                        "<td><a href='javascript:void(0);' class='tablelink' onclick='updateProductDetails(this)'>修改</a>" +
                        "<a href='javascript:void(0);' class='tablelink' onclick=\"tipOpen('是否确认注销此条信息？')\" id='cancerOrSure'>启动</a>" +
                        "</td></tr>";
                }else{
                    str += "<tr>" +
                        "<td>" + r.data[i].productId + "</td>" +
                        "<td>" + r.data[i].brandName + "</td>" +
                        "<td>" + r.data[i].typeName + "</td>" +
                        "<td>" + r.data[i].productModel + "</td>" +
                        "<td>" + r.data[i].productUnit+"</td>" +
                        "<td>可用</td>" +
                        "<td>" + getFormtTime(r.data[i].createTime,false) + "</td>" +
                        "<td>" + r.data[i].uname + "</td>" +
                        "<td><a href='javascript:void(0);' class='tablelink' onclick='updateProductDetails(this)'>修改</a>" +
                        "<a href='javascript:void(0);' class='tablelink' onclick=\"tipOpen('是否确认注销此条信息？')\" id='cancerOrSure'>注销</a>" +
                        "</td></tr>";
                }
            }
            $(".tablelist").html(str);
            $("#totalRows").html(r.totalRows);
            totalrows = $("#totalRows").html();
            $("#pagenum").html(r.pageNum);
            pagenum = $("#pagenum").html();

            $(".tablelink").click(function () {

                var text = $(this).text();
                var pId = $(this).parents("tr")[0].children[0].innerHTML
                var pStatus = $(this).parents("tr")[0].children[5].innerHTML
                if (text == "注销") {
                    $("#sure").click(function () {
                        $.ajax({
                            type: "post",
                            url: "/product/updateProductStatus/",
                            data: {pId:pId,pStatus:pStatus},
                            dataType: "json",
                            success: function (r) {
                                if (r == "true") {
                                    alert("注销成功");
                                    $("#cancerOrSure").html("启动");
                                } else {
                                    alert("注销失败");
                                }
                            }
                        })
                    })
                }
                if (text == "启动") {
                    $("#sure").click(function () {
                        $.ajax({
                            type: "post",
                            url: "/product/updateProductStatus/",
                            data: {pId:pId,pStatus:pStatus},
                            dataType: "json",
                            success: function (r) {
                                if (r == "true") {
                                    alert("启动成功");
                                    $("#cancerOrSure").html("注销");
                                    window.reload();
                                } else {
                                    alert("启动失败");
                                }
                            }
                        })
                    })
                }
            });

        }
    })
}

function shangyiye(){

    if (Number(pagenum)<=1){
        alert("已经是第一页了");
    }else{
        n = pagenum-1;
        queryAllProducts();
    }
}

function xiayiye(){
    if (Number(pagenum)>=Number(totalPage)){
        alert("已经是最后一页了");
    }else{
        n = Number(pagenum)+1;
        queryAllProducts();
    }
}

/**
 * 查询单个数据通过多个条件模糊查询
 */
function queryByCondition(){
    var sta = $(".select_status option:selected").text();
    var type = $("#type option:selected").text();
    var brand = $("#brand option:selected").text();
    var mode = $(".mode").val();
    var statu;
    if (sta=="可用"){
        statu = 1;
    }else if(sta=="不可用"){
        statu = 0;
    }else if (sta=="请选择"){
        statu = "";
    }

    $.ajax({
        url:"/product/queryOneProduct",
        type:"post",
        data:{pagenum:n,type:type,brand:brand,mode:mode,statu:statu},
        dataType:"json",
        success:function (r){
            totalPage = r.totalPage;
            var str = "<tr>" +
                "          <th>序号</th>" +
                "          <th>品牌名称</th>" +
                "          <th>商品类型</th>" +
                "          <th>商品型号</th>" +
                "          <th>单位</th>" +
                "          <th>状态</th>" +
                "          <th>创建时间</th>" +
                "          <th>创建人</th>" +
                "          <th>操作</th>" +
                "        </tr>";
            for (let i = 0; i < r.data.length; i++) {
                var status = r.data[i].productStatus;
                if (status=="0") {
                    str += "<tr>" +
                        "<td>" + r.data[i].productId + "</td>" +
                        "<td>" + r.data[i].brandName + "</td>" +
                        "<td>" + r.data[i].typeName + "</td>" +
                        "<td>" + r.data[i].productModel + "</td>" +
                        "<td>" + r.data[i].productUnit+"</td>" +
                        "<td>不可用</td>" +
                        "<td>" + getFormtTime(r.data[i].createTime,false) + "</td>" +
                        "<td>" + r.data[i].uname + "</td>" +
                        "<td><a href='javascript:void(0);' class='tablelink' onclick='updateProductDetails(this)'>修改</a>" +
                        "<a href='javascript:void(0);' class='tablelink' onclick=\"tipOpen('是否确认注销此条信息？')\" id='cancerOrSure'>启动</a>" +
                        "</td></tr>";
                }else{
                    str += "<tr>" +
                        "<td>" + r.data[i].productId + "</td>" +
                        "<td>" + r.data[i].brandName + "</td>" +
                        "<td>" + r.data[i].typeName + "</td>" +
                        "<td>" + r.data[i].productModel + "</td>" +
                        "<td>" + r.data[i].productUnit+"</td>" +
                        "<td>可用</td>" +
                        "<td>" + getFormtTime(r.data[i].createTime,false) + "</td>" +
                        "<td>" + r.data[i].uname + "</td>" +
                        "<td><a href='javascript:void(0);' class='tablelink' onclick='updateProductDetails(this)'>修改</a>" +
                        "<a href='javascript:void(0);' class='tablelink' onclick=\"tipOpen('是否确认注销此条信息？')\" id='cancerOrSure'>注销</a>" +
                        "</td></tr>";
                }
            }
            $(".tablelist").html(str);
            $("#totalRows").html(r.totalRows);
            totalrows = $("#totalRows").html();
            $("#pagenum").html(r.pageNum);
            pagenum = $("#pagenum").html();

            $(".tablelink").click(function () {

                var text = $(this).text();
                var pId = $(this).parents("tr")[0].children[0].innerHTML
                var pStatus = $(this).parents("tr")[0].children[5].innerHTML
                if (text == "注销") {
                    $("#sure").click(function () {
                        $.ajax({
                            type: "post",
                            url: "/product/updateProductStatus/",
                            data: {pId:pId,pStatus:pStatus},
                            dataType: "json",
                            success: function (r) {
                                if (r == "true") {
                                    alert("注销成功");
                                    $("#cancerOrSure").html("启动");
                                    window.reload();
                                } else {
                                    alert("注销失败");
                                }
                            }
                        })
                    })
                }
                if (text == "启动") {
                    $("#sure").click(function () {
                        $.ajax({
                            type: "post",
                            url: "/product/updateProductStatus/",
                            data: {pId:pId,pStatus:pStatus},
                            dataType: "json",
                            success: function (r) {
                                if (r == "true") {
                                    alert("启动成功");
                                    $("#cancerOrSure").html("注销");
                                    window.reload();
                                } else {
                                    alert("注销失败");
                                }
                            }
                        })
                    })
                }
            });
        }
    })
}

function queryType(){

    $.ajax({
        url:"/product/queryType",
        type:"post",
        data:{},
        dataType:"json",
        success:function (result){
            var str = "<option selected='selected' value='0'>请选择</option>";
            for (var i = 0; i < result.length; i++) {
                str += "<option value='" + result[i].brandId + "'>"+result[i].typeName+"</option>";
            }
            $("#type").html(str);
        }
    })
}
let id;
function queryBrand(){

    var id = $("#type option:selected").val();
    $.ajax({
        url:"/product/queryBrand",
        type:"post",
        data:{id:id},
        dataType:"json",
        success:function (result){
            var str = "<option selected='selected' value='0'>请选择</option>";
            for (var i = 0; i < result.length; i++) {
                str += "<option>"+result[i].brandName+"</option>";
            }

            $("#brand").html(str);
        }
    })
}


function updateProductDetails(e){
    var id = $(e).parents("tr")[0].children[0].innerHTML;;

    var brand = $(e).parents("tr")[0].children[1].innerHTML;
    var type = $(e).parents("tr")[0].children[2].innerHTML;
    var mode = $(e).parents("tr")[0].children[3].innerHTML;
    var unit = $(e).parents("tr")[0].children[4].innerHTML;
    var statu = $(e).parents("tr")[0].children[5].innerHTML;
    var creatTime = $(e).parents("tr")[0].children[6].innerHTML;
    var creatPeople = $(e).parents("tr")[0].children[7].innerHTML;

    var ck = $(e).html();

    location.replace("productUpdate.html");
//设置sessionStorage：-----------主要代码-------------------------------
    window.sessionStorage.setItem('id', id);
    window.sessionStorage.setItem('brand', brand);
    window.sessionStorage.setItem('type', type);
    window.sessionStorage.setItem('mode', mode);
    window.sessionStorage.setItem('creatTime', creatTime);
    window.sessionStorage.setItem('creatPeople', creatPeople);
    window.sessionStorage.setItem('ck', ck);
    window.sessionStorage.setItem('statu', statu);
    window.sessionStorage.setItem('unit', unit);

}



